/*
 * Power.java
 *
 * Copyright 2008 the Desert Labs Project (http://desertlabs.us)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package us.desertlabs.physics;

import java.math.BigDecimal;
import us.desertlabs.number.BigUtility;

/**
 * This is used to represent an amount of power. Power refers to an amount of
 * energy for an amount of time.
 *
 * @author Brandon Franklin
 */
public class Power extends SimpleUnit {

    private static final long serialVersionUID = 3257848783512287026L;

    /**
     * Given an <CODE>Energy</CODE> and <CODE>Time</CODE>, calculates and
     * returns the corresponding amount of <CODE>Power</CODE>.
     *
     * @param energy the <CODE>Energy</CODE> being expended in total
     * @param time the <CODE>Time</CODE> over which the expenditure occurs
     * @return the <CODE>Power</CODE> resulting from expending the <CODE>Energy</CODE>
     *         for the supplied <CODE>Time</CODE>
     * @throws IllegalArgumentException if either of the parameters is invalid
     *         for any reason
     */
    static public Power calculate( final Energy energy, final Time time ) {

        // Validate the energy
        if( energy == null ) {
            throw new IllegalArgumentException( "Energy cannot be null." );
        }

        // Validate the time
        if( time == null ) {
            throw new IllegalArgumentException( "Time cannot be null." );
        }

        BigDecimal bigDecEnergy = new BigDecimal( energy.toString() );
        bigDecEnergy = BigUtility.normalizeScale( bigDecEnergy );
        final BigDecimal bigDecTime = new BigDecimal( time.toString() );
        final BigDecimal result = bigDecEnergy.divide( bigDecTime, BigDecimal.ROUND_HALF_UP );

        return new Power( result );
    }

    /**
     * Creates a new instance of <CODE>Power</CODE> representing the specified
     * amount of power.
     *
     * @param amount the amount of power to be represented
     * @throws IllegalArgumentException if the parameter is invalid for any
     *         reason
     */
    public Power( final Number amount ) {
        super( amount );
    }
}
